import request from '../plugins/request';

interface UserInfo {
    userId: number;
    username: string;
    avatarUrl: string;
    gender?: number;
    tags?: string;
}

interface CommentInfo {
    commentId: number;
    content: string;
    likeCount: number;
    user: {
        userId: number;
        username: string;
        avatarUrl: string;
    };
}

interface DynamicData {
    dynamicId: number;
    userId: number;
    content: string;
    mediaUrls?: string[];
    likeCount: number;
    commentCount: number;
    shareCount: number;
    createTime: string;
    hasLiked?: boolean;
    isLiked?: boolean;
    user: UserInfo;
    hotComments?: CommentInfo[];
}

interface CommentData {
    commentId: number;
    content: string;
    likeCount: number;
    user: {
        userId: number;
        username: string;
        avatarUrl: string;
    };
    createTime: string;
    isLiked?: boolean;
}

interface ApiResponse<T> {
    code: number;
    message?: string;
    data: T;
    description?: string;
}

interface PageResult<T> {
    records: T[];
    total: number;
    size: number;
    current: number;
    pages: number;
}

// 动态相关接口
export const createDynamic = (data: { 
    content: string; 
    mediaUrls?: string[]
}): Promise<ApiResponse<number>> => {
    return request.post('/dynamic/add', data);
};

export const deleteDynamic = (id: number) => {
    return request.post(`/dynamic/delete/${id}`);
};

// 获取动态详情
export const getDynamicDetail = (id: number) => {
    return request.get(`/dynamic/${id}`);
};

// 分页获取动态列表
export const getDynamicList = (params: { 
    current?: number; 
    pageSize?: number;
    userId?: number;
    content?: string;
    sortField?: string;
    sortOrder?: string;
} = {}) => {
    return request.get('/dynamic/list/page', { params });
};

// 获取当前用户的动态列表
export const getMyDynamicList = (params: {
    current?: number;
    pageSize?: number;
    content?: string;
    sortField?: string;
    sortOrder?: string;
} = {}): Promise<ApiResponse<PageResult<DynamicData>>> => {
    return request.get('/dynamic/my/list', { params });
};

// 点赞动态
export const likeDynamic = (id: number) => {
    return request.post(`/dynamic/like/${id}`);
};

// 取消点赞动态
export const unlikeDynamic = (id: number) => {
    return request.post(`/dynamic/unlike/${id}`);
};

// 评论相关接口
export const addComment = (data: { 
    dynamicId: number; 
    content: string; 
    parentId?: number | null
}): Promise<ApiResponse<any>> => {
    return request.post('/comment/add', data);
};

export const deleteComment = (commentId: number): Promise<ApiResponse<boolean>> => {
    return request.post(`/comment/delete/${commentId}`);
};

export const getCommentList = (dynamicId: number, params: { 
    current?: number; 
    pageSize?: number 
} = {}): Promise<ApiResponse<{
    records: any[];
    total: number;
    size: number;
    current: number;
    pages: number;
}>> => {
    return request.get(`/comment/list/dynamic/${dynamicId}`, { params });
};

// 点赞相关接口
export const likeAction = (data: { 
    dynamicId?: number; 
    commentId?: number; 
    type: number 
}): Promise<ApiResponse<boolean>> => {
    return request.post('/like/action', data);
};

export const isLiked = (params: { 
    dynamicId?: number; 
    commentId?: number; 
    type: number 
}): Promise<ApiResponse<boolean>> => {
    return request.get('/like/is_liked', { params });
};

// 分享相关接口
export const addShare = (data: { 
    dynamicId: number; 
    type: number; 
    content?: string 
}): Promise<ApiResponse<number>> => {
    return request.post('/share/add', data);
};

export const deleteShare = (shareId: number): Promise<ApiResponse<boolean>> => {
    return request.post(`/share/delete/${shareId}`);
};